package com.taiji.dianthus.dao.sys;

import com.taiji.dianthus.domain.sys.DSysOrg;
import org.hibernate.annotations.Parent;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName OrgRepository
 * @Description
 * @Author H.M
 * @Date 2019/9/26
 */
@Repository
public interface OrgRepository extends JpaRepository<DSysOrg, String>, JpaSpecificationExecutor<DSysOrg> {

    /**
     * 逻辑删除
     *
     * @param id
     */
    @Modifying
    @Transactional
    @Query("update DSysOrg p set p.enableFlag = 0  where p.id=:id")
    void updateFlag(@Param("id") String id);


    /**
     * 根据ID 获取组织
     *
     * @param id
     * @return
     */
    @Query("select p from  DSysOrg p where  p.id=:id and p.enableFlag = 1 ")
    DSysOrg getById(@Param("id") String id);

    /**
     * 查询
     *
     * @param orgId
     * @return
     */
    @Query("select count(u.id) from  DSysOrg u where u.parentId =:orgId and u.enableFlag=1")
    Integer countByParentId(@Param("orgId") String orgId);

    /**
     * 查询所有
     *
     * @return
     */
    @Query("select p from  DSysOrg p where  p.enableFlag = 1 and p.id <> 'ff80808174950f1f017495aa00aa0003' order by p.orgOrder asc ")
    List<DSysOrg> listAll();

    @Query(value = "select p.* from  d_sys_org p where  p.enable_flag = 1 and p.id != :orgId ",
            countQuery = "select count(*) from  d_sys_org p where  p.enable_flag = 1 and p.id != :orgId ",
            nativeQuery = true)
    List<DSysOrg> listAllExclusionId(@Param("orgId") String orgId);

    /**
     * 根据组织代码查询
     *
     * @param pushId
     * @return
     */
    @Query("select p from  DSysOrg p where p.pushId =:pushId and  p.enableFlag = 1")
    DSysOrg getByPushId(@Param("pushId") String pushId);

    /**
     * 查询下级节点
     *
     * @param parentId
     * @return
     */
    @Query("select org from DSysOrg org where org.parentId =:parentId")
    List<DSysOrg> findByParentCode(@Param("parentId") String parentId);

    /**
     * 根据id数组查询集合
     *
     * @param ids
     * @return
     */
    @Query("select t from DSysOrg t where t.id in :ids and  t.enableFlag = 1 ")
    List<DSysOrg> listByIdArray(@Param("ids") String[] ids);

    /**
     * 根据id数组查询集合
     *
     * @param ids
     * @return
     */
    @Query("select t from DSysOrg t where t.id  in :ids  and  t.enableFlag = 1 ")
    List<DSysOrg> listByIdList(@Param("ids") List<String> ids);

    /**
     * 查询所有组织
     *
     * @return
     */
    @Query("select count(t) from DSysOrg  t where  t.enableFlag = 1")
    Integer countOrg();

    @Query("select count(t) from DSysOrg  t where  t.enableFlag = 1 and t.orgName=:orgName and t.parentId=:parentId")
    Integer getOrgByOrgName(@Param("orgName") String orgName, @Param("parentId") String parentId);

    @Query("select t from DSysOrg  t where  t.enableFlag = 1 and  t.parentId=:id")
    List<DSysOrg> getZijiOrg(@Param("id") String id);

    @Query("select count(t) from DSysOrg  t where  t.enableFlag = 1 and t.orgName=:orgName and t.parentId is NULL")
    Integer getOrgOrgNameByParentNull(@Param("orgName") String orgName);

    @Query("select count(t.orgName) from DSysOrg  t where  t.orgName=:orgName and  t.parentId is NULL and t.id <>:editId")
    Integer editOrgOrgNameByParentNull(@Param("orgName") String orgName, @Param("editId") String editId);

    @Query("select count(t) from DSysOrg t where t.enableFlag=1 and t.orgName=:orgName and t.parentId=:parentId and t.id<>:editId")
    Integer editOrgByOrgName(@Param("orgName") String orgName, @Param("parentId") String parentId, @Param("editId") String editId);
}
